package com.swjd.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swjd.common.utils.Page;

import com.swjd.po.customer;
import com.swjd.po.dict;
import com.swjd.po.user;
import com.swjd.service.CustomerService;
import com.swjd.service.DictService;

@Controller
@RequestMapping("/customer")
public class CustomerController {
	@Autowired
	private CustomerService customerService;

	@Autowired
	private DictService dictService;

	// 实现点击查询的映射处理
	@RequestMapping("/list.action")
	// 绑定前端传入的查询条件 默认当前页第一页，默认10行
	public String findAll(Model model, String custName, String custSource, String custIndustry, String custLevel,
			@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows) {
		// 把条件赋给数据库对应字段
		String cust_name = custName;
		String cust_source = custSource;
		String cust_industry = custIndustry;
		String cust_level = custLevel;
		// 通过对应字段筛选查询
		Page<customer> pages = customerService.findCustomerByDict(cust_name, cust_source, cust_industry, cust_level,
				page, rows);
		// 查询条件下拉框的数据
		List<dict> fromType = dictService.findDictSource();
		List<dict> industryType = dictService.findDictIndustry();
		List<dict> levelType = dictService.findDictLevel();
		// 绑定发送到前端页面
		model.addAttribute("levelType", levelType);
		model.addAttribute("custLevel", custLevel);
		model.addAttribute("industryType", industryType);
		model.addAttribute("custIndustry", custIndustry);
		model.addAttribute("custSource", custSource);
		model.addAttribute("fromType", fromType);
		model.addAttribute("page", pages);
		return "customer";
	}

	@RequestMapping("/create.action")
	@ResponseBody
	public String addcustomer(customer customer,HttpSession session) {
		//获取当前时间存入customer
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		customer.setCust_createtime(timestamp);
		//获取session的用户信息 将用户id即创建人id 存储在客户对象中
		user user = (user) session.getAttribute("USER_SESSION");
		customer.setCust_create_id(user.getUser_id());
		//获取执行sql受影响行数
		int i = customerService.addCustomer(customer);
		//返回数据给前台页面
		if(i>0) {
		return "OK!";
		}else {
			return "false!";
		}
	}
	//把客户信息传入到修改模态框中
	@RequestMapping("/getCustomerById.action")
	@ResponseBody
	public customer findCustomerById(@RequestParam("id") int cust_id,Model model) {
		customer customer = customerService.findCustomerByid(cust_id);
		return customer;
	}
	//修改客户信息
	@RequestMapping("/update.action")
	@ResponseBody
	public String updateCustomer(customer customer) {
		int i = customerService.updateCustomerById(customer);
		if(i>0) {
			return "OK!";
		}
		return "false";
	}
	@RequestMapping("/delete.action")
	@ResponseBody
	public String deleteCustomerById(int id) {
		int i = customerService.deleteCustomerById(id);
		if(i>0) {
			return "OK!";
		}
		return "false";
	}
}
